package com.kzx.blog.util;

import com.kzx.blog.service.model.PagingParam;
import lombok.Data;

import java.util.*;

/**
 * @author kongz
 * @version 1.0 2021/12/2 14:09
 * @description 分页工具类
 */
@Data
public class PageUtils<T> {

    /*
    默认页面大小
     */
    public static final int DEFAULT_PAGE_SIZE = 10;

    /*
    当前页数
     */
    private int pageIndex;
    /*
    页面大小
     */
    private int pageSize;
    /*
    总页数
     */
    private int totalPage;
    /*
    总条数
     */
    private int totalCount;
    /*
    数据
     */
    private List<T> list;

    /**
     * sql分页查询, 将结果赋给该工具类
     * @param pageIndex 当前页码
     * @param pageSize 页面大小
     * @param totalCount 总条数
     * @param list 当前页码数据
     */
    public PageUtils(int pageIndex, int pageSize, int totalCount, List<T> list) {
        if (pageIndex == -1 && pageSize == -1) {
            this.pageIndex = pageIndex;
            this.pageSize = pageSize;
            this.totalCount = totalCount;
            this.totalPage = 1;
        } else {
            this.pageIndex = pageIndex <= 0 ? 1 : pageIndex;
            this.pageSize = pageSize <=0 ? DEFAULT_PAGE_SIZE : pageSize;
            this.totalCount = totalCount;
            this.totalPage =  this.totalCount /  this.pageSize +  (this.totalCount % this.pageSize > 0 ? 1 : 0);
        }
        this.list = list;
    }

    /**
     * list分页
     * @param pageIndex 当前页码
     * @param pageSize 页面大小
     * @param list 全部数据
     */
    public PageUtils(int pageIndex, int pageSize, List<T> list) {
        if (pageIndex == -1 && pageSize == -1) {
            this.pageIndex = pageIndex;
            this.pageSize = pageSize;
            this.totalCount = list.size();
            this.totalPage = 1;
            this.list = list;
        } else {
            this.pageIndex = pageIndex <= 0 ? 1 : pageIndex;
            this.pageSize = pageSize <= 0 ? DEFAULT_PAGE_SIZE : pageSize;
            this.totalCount = list.size();
            this.totalPage = this.totalCount % pageSize == 0 ? this.totalCount / pageSize : (this.totalCount / pageSize + 1);
            if (this.totalCount > 0) {
                if (this.pageIndex > this.totalPage) {
                    this.list = Collections.emptyList();
                } else {
                    int startIndex = this.pageSize * (this.pageIndex - 1);
                    int endIndex = 0;
                    if (this.pageSize * this.pageIndex > this.totalCount) {
                        endIndex = this.totalCount;
                    } else {
                        endIndex = this.pageSize * this.pageIndex;
                    }

                    this.list = list.subList(startIndex, endIndex);
                }
            } else {
                this.list = list;
            }
        }
    }

    /**
     * 获取分页查询参数
     * @param param pageIndex, pageSize
     * @return pageStart, pageSize
     */
    public static Map<String, Object> getPagingMap(PagingParam param) {
        Map<String, Object> map = new HashMap<>();

        if (param.getPageIndex() != -1) {
            int size;
            if (param.getPageSize() <= 0) {
                size = DEFAULT_PAGE_SIZE;
            } else {
                size = param.getPageSize();
            }
            map.put("pageSize", size);
            map.put("pageStart", (param.getPageIndex() - 1) * size);
        }
        return map;
    }
}
